package com.gmail.huxiaowi.cblog.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.gmail.huxiaowi.cblog.util.Utils;
import com.google.appengine.api.datastore.Text;

/**
 * Class Article.
 * 
 *   文章POJO
 *   
 * @author  huxw
 * @version $Revision:1.0.0, $Date:2010-4-29 下午05:40:43$
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Article implements Serializable{

    private static final long serialVersionUID = -3345181127740481388L;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    
    @Persistent
    private String title;
    
    @NotPersistent
    private String description;
    
    @Persistent
    private Text content;
    
    @Persistent
    private Date dateline;
    
    @Persistent
    private String postName;
    
    @Persistent
    private int browseCount;
    
    @Persistent
    private int messageCount;
    
    @Persistent
    private Long categoryId;
    
    @Persistent
    private Set<String> ips = new HashSet<String>();
    
    public Article() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Article(String postName, String title, String content, Date dateline) {
        super();
        this.postName = postName;
        this.title = title;
        this.content = new Text(content);
        this.dateline = dateline;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        description = Utils.intercept(content);
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Text getContent() {
        return content;
    }

    public void setContent(Text content) {
        this.content = content;
    }

    public Date getDateline() {
        return dateline;
    }

    public void setDateline(Date dateline) {
        this.dateline = dateline;
    }

    public int getBrowseCount() {
        return browseCount;
    }

    public void setBrowseCount(int browseCount) {
        this.browseCount = browseCount;
    }
    
    public void addIP(String ip) {
        this.ips.add(ip);
        this.browseCount = this.ips.size();
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public Set<String> getIps() {
        return ips;
    }

    public void setIps(Set<String> ips) {
        this.ips = ips;
    }
    
    public void addMessage(Message message) {
        message.setArticleId(id);
        messageCount++;
    }

    public int getMessageCount() {
        return messageCount;
    }

    public void setMessageCount(int messageCount) {
        this.messageCount = messageCount;
    }

    public String getPostName() {
        return postName;
    }

    public void setPostName(String postName) {
        this.postName = postName;
    }
}
